﻿using System;
using System.IO;
using System.Web;
using Common;
using System.Collections.Generic;
using Common.QHWShop;

namespace Common.QHWShop
{
    public class ImgFileUpload
    {

        /// <summary>
        /// 获取各种大小图片的http请求路径
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string GetUploadWebUrl(string fileName, int width, int height)
        {
            string serverPath;


            if (string.IsNullOrEmpty(fileName))
            {
                return "";
            }
            
            if (width != 0)
            {
                fileName = fileName.Trim(System.IO.Path.GetExtension(fileName).ToCharArray()) + ".jpeg";

            }
            string firstChars = fileName.Substring(0, 2);
            ///获取服务器路径
            Common.XmlHelper.SearchXml search = delegate(string value)
            {
                if (value == "all" || value.Contains(firstChars))
                    return true;
                else
                    return false;
            };

            serverPath = XmlHelper.Read(HttpContext.Current.Server.MapPath("~/App_Data/FileServerConfig.xml"), "/serverList/server", search, "number", "webUrl");

            if (width > 0 && height > 0)
            {
                firstChars = firstChars + width.ToString() + height.ToString();
            }
            string secondChars = fileName.Substring(3, 1);
            string other = fileName.Substring(4, 5);
            int thirdRoot = 0;

            if (int.TryParse(other, System.Globalization.NumberStyles.HexNumber, null, out thirdRoot))
            {
                string thirdChars = (int.Parse(other, System.Globalization.NumberStyles.HexNumber) % 1024).ToString();
                return serverPath.TrimEnd('/') + "/" + firstChars + "/" + secondChars + "/" + thirdChars + "/" + fileName;
            }
            else
            {
                return "";
            }
        }

    }
}
